﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace RSSReader
{
    public partial class frmPostAlarm : Form
    {
        private List<RssItem> _updatePosts = new List<RssItem>();
        bool bFormLoad = false;

        public frmPostAlarm()
        {
            InitializeComponent();
        }

        public frmPostAlarm(List<RssItem> list)
        {
            InitializeComponent();
            _updatePosts = list;
            _treeBlogPost.ImageList = new ImageList();
            _treeBlogPost.ImageList.Images.Add(global::RSSReader.Properties.Resources.channel);
            _treeBlogPost.ImageList.Images.Add(global::RSSReader.Properties.Resources.item);
        }

        void treeBlogPost_AfterSelect(object sender, TreeViewEventArgs e)
        {
            try
            {
                //title노드가 있다면 포스트를 가져옵니다.
                if (e.Node.Parent != null)
                {
                    string contents = "<style> <!-- body { margin: 10; padding: 0; font-size: 12px; line-height: 1.5em; } IMG { BORDER : 0; } A:LINK { TEXT-DECORATION: NONE; BORDER : 0; } A:VISITED { TEXT-DECORATION: NONE; BORDER : 0; }--> </style>";
                    contents += "<b><h2><a href=" + e.Node.Name + ">" + e.Node.Text + "</a></h2></b><hr>";
                    contents += e.Node.Tag.ToString();
                    webBrowser.DocumentText = contents;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, ex.Source, MessageBoxButtons.OK);
            }
        }

        private void Init()
        {
            TreeNode topNode = new TreeNode("New Posts");
            _treeBlogPost.Nodes.Clear();
            _treeBlogPost.Nodes.Add(topNode);

            foreach (RssItem item in _updatePosts)
            {
                TreeNode node = new TreeNode(item.Title);
                node.Name = item.Link;
                node.Tag = item.Description;
                topNode.Nodes.Add(node);
            }
            topNode.Expand();
        }

        private void frmPostAlarm_Load(object sender, EventArgs e)
        {
            if (!bFormLoad)
            {
                _treeBlogPost.AfterSelect += new TreeViewEventHandler(treeBlogPost_AfterSelect);
                Init();
                bFormLoad = true;
            }
        }

        private void frmPostAlarm_Activated(object sender, EventArgs e)
        {
            if (_treeBlogPost.Nodes.Count != 0)
                _treeBlogPost.Nodes[0].Expand();
        }
    }
}